//路由鉴权：就是路由能不能被访问到权限设置-》全局守卫完成
import router from "./router";
import pinia from "@/store";
//引入进度条
import Nprogress from "nprogress";
import "nprogress/nprogress.css";
import useUserStore from "./store/modules/user";
let userStore = useUserStore(pinia);
//存储用户未登录可以访问的路径
let whiteList = [
  "/home",
  "/hospital/register",
  "/hospital/detail",
  "/hospital/notice",
  "/hospital/close",
  "/hospital/search",
];
Nprogress.configure({ showSpinner: false });
//添加相应的全局守卫
// 前置守卫
router.beforeEach((to, from, next) => {
  //访问路由组件之前，进度条开始动
  Nprogress.start();
  //动态设置网页左上角的标题
  document.title = `尚医通-${to.meta.title}`;
  //判断用户是否登录--token
  let token = userStore.userInfo.token;
  if (token) {
    next();
  } else {
    if (whiteList.includes(to.path)) {
      next();
    } else {
      userStore.visible = true;
      next({
        path: "/home",
        query: {
          redirect: to.fullPath,
        },
      });
    }
  }
});
//后置守卫
router.afterEach((to, from) => {
  //访问路由组件成功进度条消失
  Nprogress.done();
});
